//
//  LSRTagViewController.m
//  BaiSiProject
//
//  Created by lshenrong on 16/7/7.
//  Copyright © 2016年 lshenrong. All rights reserved.
//

#import "LSRTagViewController.h"
#import "LSRTagViewCell.h"
#import "LSRTagItem.h"
#import <AFNetworking.h>
#import <SVProgressHUD.h>


#import <MJExtension/MJExtension.h>

@interface LSRTagViewController ()

@property(strong ,nonatomic)NSMutableArray *tagArray;


@property(weak,nonatomic) AFHTTPSessionManager *mgr;

@end

@implementation LSRTagViewController


-(NSMutableArray *)tagArray
{
    if (!_tagArray) {
        
        _tagArray=[NSMutableArray array];
    }


    return _tagArray ;


}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置cell的高度
    self.tableView.rowHeight=60;
    //加载数据,调用loadData方法
   [self loadData];
    //设置背景颜色，用作改变frame显示分割线
    self.tableView.backgroundColor = [UIColor redColor];
    //取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [SVProgressHUD showWithStatus:@"正在加载..."];

    
    
//   self.tableView.zStyle
}



-(void)loadData
{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    
    self.mgr=manager;
    
    NSMutableDictionary *parameter=[NSMutableDictionary dictionary];
    
    /*a	true	string	tag_recommend
     action	true	string	sub
    c	true	string	topic
   */
    parameter[@"a"]=@"tag_recommend";
    parameter[@"action"]=@"sub";
    parameter[@"c"]=@"topic";
    
    
    
        
        [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameter success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
             //请求成功了，取消提示
            [SVProgressHUD dismiss];
            
            //f返回体是一个字典数组
            NSArray *array=responseObject;//这里也可以用NSArray，父类指针指向子类
            
            //通过字典数组来创建模型数组，是数组！不是模型
            
            self.tagArray=[LSRTagItem mj_objectArrayWithKeyValuesArray:array];
            
            [self.tableView reloadData];
            //         写进plist文件来观察，得出是个字典数组
            //        [responseObject writeToFile:@"/Users/lshenrong/Desktop/data/tag.plist" atomically:YES];
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            //同样，请求失败了也要取消提示
            [SVProgressHUD dismiss];
            
            NSLog(@"%@",error);
            
            
            
        }];
        
    
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [SVProgressHUD dismiss];
    //取消请求，进入这个方法，是这种情形，请求一直不成功，用户点击返回
    [self.mgr.tasks makeObjectsPerformSelector:@selector(cancel)];

}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
     NSLog(@"%@",self.tagArray);
    return self.tagArray.count;
    
   

    
   

//    return 20;
}


- (LSRTagViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //重用标识
   static NSString *ID = @"tag";
    //最好删掉全部重新再写，原来的dequeue方法不适合
    LSRTagViewCell *cell =[tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        
        cell=[LSRTagViewCell tagWithCell];
    }
    
    
     cell.item=self.tagArray[indexPath.row];
    
    NSLog(@"%@",cell.item);

    
     return cell;


   
}



@end
